
import store from '../lib/store';
	
	
/** 组 */
const useGroup = () => {
	/** 词汇相关共享数据 */
	const storeData = store();

	
	return {
		/** 
		 * @description 计算
		 * @param { Record<string, any>[] } grouplist 分组列表
		 * @param { (index: number, indexs: number[]) => boolean } isExist 检测是否在当前组
		*/
		calculate: (grouplist: Record<string, any>[], isExist: (index: number, indexs: number[]) => boolean) => {
			const query = {
				title: '',
				words: []
			};
			
			const resetlist = (numbers: number[]) => {
				query.words = [];
				grouplist?.forEach?.((item, ele) => {
					if (isExist(ele, numbers)) {
						query.words.push(...item.map((_item: Record<string, any>) => ({ ..._item })));
					};
				});
			};
			
			switch(grouplist.length) {
				case 2: {
					query.title = storeData.shearsBox.mixGroup === 1 ? '第1、2组' : '全部混组';
					break;
				}
				
				case 3:
				case 4:
				case 5:
				case 6:
				case 7:
				case 8:
				case 9: {
					switch(+storeData.shearsBox.mixGroup) {
						case 1: {
							query.title = '第1、2组';
							break;
						}
						
						case 2: {
							query.title = '第1、2、3组';
							break;
						}
						
						default: {
							switch(true) {
								case [5, 6, 7, 8, 9].includes(grouplist.length) && storeData.shearsBox.mixGroup === 3: {
									resetlist([3, 4]);
									query.title = '第4、5组';
									break;
								}
								
								case [6, 7, 8, 9].includes(grouplist.length) && storeData.shearsBox.mixGroup === 4: {
									resetlist([3, 4, 5]);
									query.title = '第4、5、6组'
									break;
								}
								
								case [7, 8, 9].includes(grouplist.length) && storeData.shearsBox.mixGroup === 5: {
									resetlist([0, 1, 2, 3, 4, 5]);
									query.title = '第1、2、3、4、5、6组';
									break;
								}
								
								case [8, 9].includes(grouplist.length) && storeData.shearsBox.mixGroup === 6: {
									resetlist([6, 7]);
									query.title =  '第7、8组';
									break;
								}
								
								case grouplist.length === 9 && storeData.shearsBox.mixGroup === 7: {
									resetlist([6, 7, 8]);
									query.title =  '第7、8、9组';
									break;
								}
								
								default: {
									query.title = '全部混组';
									break;
								}
							};
							
							break;
						}
					};
					
					break;
				}
			};
			
			
			return query;
		}
	};
};


export default useGroup;